
#include "com.h"
#include "cmsis_os2.h"
#include "stdio.h"


struct UART uart[SYS_COM_NUM];

void COM_RS232_Debug_Rx_IrqCallback(void);
void COM_RS232_Debug_Tx_IrqCallback(void);
void COM_RS232_Rx_IrqCallback(void);
void COM_RS232_Tx_IrqCallback(void);
void COM_RS485_1_Rx_IrqCallback(void);
void COM_RS485_1_Tx_IrqCallback(void);
void COM_RS485_2_Rx_IrqCallback(void);
void COM_RS485_2_Tx_IrqCallback(void);
void USART1_IRQHandler(void)
{
	if(usart_interrupt_flag_get(USART1, USART_RDBF_FLAG) != RESET)
  {
    COM_RS232_Rx_IrqCallback();
  }
  if(usart_interrupt_flag_get(USART1, USART_TDBE_FLAG) != RESET)
  {
    COM_RS232_Tx_IrqCallback();
  }
//	usart_flag_clear(USART1, uint32_t flag);
}
void USART2_IRQHandler(void)
{
	if(usart_interrupt_flag_get(USART2, USART_RDBF_FLAG) != RESET)
  {
    COM_RS232_Debug_Rx_IrqCallback();
  }
  if(usart_interrupt_flag_get(USART2, USART_TDBE_FLAG) != RESET)
  {
    COM_RS232_Debug_Tx_IrqCallback();
  }
//	usart_flag_clear(USART1, uint32_t flag);
}
void USART3_IRQHandler(void)
{
	if(usart_interrupt_flag_get(USART3, USART_RDBF_FLAG) != RESET)
  {
    COM_RS485_1_Rx_IrqCallback();
  }
  if(usart_interrupt_flag_get(USART3, USART_TDBE_FLAG) != RESET)
  {
    COM_RS485_1_Tx_IrqCallback();
  }
//	usart_flag_clear(USART1, uint32_t flag);
}
void USART6_IRQHandler(void)
{
	if(usart_interrupt_flag_get(USART6, USART_RDBF_FLAG) != RESET)
  {
    COM_RS485_2_Rx_IrqCallback();
  }
  if(usart_interrupt_flag_get(USART6, USART_TDBE_FLAG) != RESET)
  {
    COM_RS485_2_Tx_IrqCallback();
  }
//	usart_flag_clear(USART6, uint32_t flag);
}

void COM_RS232_Debug_Rx_IrqCallback(void)
{
	unsigned char temp;
	temp = temp;
	if(uart[COM_RS232_Debug].rec_full)
	{
		temp = usart_data_receive(uart[COM_RS232_Debug].USARTx);
	}
	else
	{
		uart[COM_RS232_Debug].rec_fifo[uart[COM_RS232_Debug].rec_end] = usart_data_receive(uart[COM_RS232_Debug].USARTx);
		uart[COM_RS232_Debug].rec_end++;
		uart[COM_RS232_Debug].rec_end %= UART_FIFO_SIZE;
		uart[COM_RS232_Debug].rec_null = 0;
		if ((uart[COM_RS232_Debug].rec_end) == uart[COM_RS232_Debug].rec_head)
		{
			uart[COM_RS232_Debug].rec_full = 1;
		}
	}
}
void COM_RS232_Debug_Tx_IrqCallback(void)
{
	if(1)// while(__HAL_UART_GET_FLAG(&huart8, UART_IT_TXE)==SET)
  {
		if(uart[COM_RS232_Debug].send_null)
		{
			usart_interrupt_enable(uart[COM_RS232_Debug].USARTx, USART_TDBE_INT, FALSE);// ��ֹ�����ж�
			uart[COM_RS232_Debug].txstate=COM_STATE_READY_TX;
		}
		else
		{
			usart_data_transmit(uart[COM_RS232_Debug].USARTx, uart[COM_RS232_Debug].send_fifo[uart[COM_RS232_Debug].send_head]);
			uart[COM_RS232_Debug].send_head++;
			uart[COM_RS232_Debug].send_head %= UART_FIFO_SIZE;
			uart[COM_RS232_Debug].send_full = 0;
			if (uart[COM_RS232_Debug].send_head == uart[COM_RS232_Debug].send_end)
			{
				uart[COM_RS232_Debug].send_null = 1;
			}
		}
	}
}
void COM_RS232_Rx_IrqCallback(void)
{
	unsigned char temp;
	temp = temp;
	if(uart[COM_RS232].rec_full)
	{
		temp = usart_data_receive(uart[COM_RS232].USARTx);
	}
	else
	{
		uart[COM_RS232].rec_fifo[uart[COM_RS232].rec_end] = usart_data_receive(uart[COM_RS232].USARTx);
		uart[COM_RS232].rec_end++;
		uart[COM_RS232].rec_end %= UART_FIFO_SIZE;
		uart[COM_RS232].rec_null = 0;
		if ((uart[COM_RS232].rec_end) == uart[COM_RS232].rec_head)
		{
			uart[COM_RS232].rec_full = 1;
		}
	}
}
void COM_RS232_Tx_IrqCallback(void)
{
	if(1)// while(__HAL_UART_GET_FLAG(&huart8, UART_IT_TXE)==SET)
  {
		if(uart[COM_RS232].send_null)
		{
			usart_interrupt_enable(uart[COM_RS232].USARTx, USART_TDBE_INT, FALSE);// ��ֹ�����ж�
			uart[COM_RS232].txstate=COM_STATE_READY_TX;
		}
		else
		{
			usart_data_transmit(uart[COM_RS232].USARTx, uart[COM_RS232].send_fifo[uart[COM_RS232].send_head]);
			uart[COM_RS232].send_head++;
			uart[COM_RS232].send_head %= UART_FIFO_SIZE;
			uart[COM_RS232].send_full = 0;
			if (uart[COM_RS232].send_head == uart[COM_RS232].send_end)
			{
				uart[COM_RS232].send_null = 1;
			}
		}
	}
}
void COM_RS485_1_Rx_IrqCallback(void)
{
	unsigned char temp;
	temp = temp;
	if(uart[COM_RS485_1].rec_full)
	{
		temp = usart_data_receive(uart[COM_RS485_1].USARTx);
	}
	else
	{
		uart[COM_RS485_1].rec_fifo[uart[COM_RS485_1].rec_end] = usart_data_receive(uart[COM_RS485_1].USARTx);
		uart[COM_RS485_1].rec_end++;
		uart[COM_RS485_1].rec_end %= UART_FIFO_SIZE;
		uart[COM_RS485_1].rec_null = 0;
		if ((uart[COM_RS485_1].rec_end) == uart[COM_RS485_1].rec_head)
		{
			uart[COM_RS485_1].rec_full = 1;
		}
	}
}
void COM_RS485_1_Tx_IrqCallback(void)
{
	if(1)// while(__HAL_UART_GET_FLAG(&huart8, UART_IT_TXE)==SET)
  {
		if(uart[COM_RS485_1].send_null)
		{
			usart_interrupt_enable(uart[COM_RS485_1].USARTx, USART_TDBE_INT, FALSE);// ��ֹ�����ж�
			uart[COM_RS485_1].txstate=COM_STATE_READY_TX;
		}
		else
		{
			usart_data_transmit(uart[COM_RS485_1].USARTx, uart[COM_RS485_1].send_fifo[uart[COM_RS485_1].send_head]);
			uart[COM_RS485_1].send_head++;
			uart[COM_RS485_1].send_head %= UART_FIFO_SIZE;
			uart[COM_RS485_1].send_full = 0;
			if (uart[COM_RS485_1].send_head == uart[COM_RS485_1].send_end)
			{
				uart[COM_RS485_1].send_null = 1;
			}
		}
	}
}
void COM_RS485_2_Rx_IrqCallback(void)
{
	unsigned char temp;
	temp = temp;
	if(uart[COM_RS485_2].rec_full)
	{
		temp = usart_data_receive(uart[COM_RS485_2].USARTx);
	}
	else
	{
		uart[COM_RS485_2].rec_fifo[uart[COM_RS485_2].rec_end] = usart_data_receive(uart[COM_RS485_2].USARTx);
		uart[COM_RS485_2].rec_end++;
		uart[COM_RS485_2].rec_end %= UART_FIFO_SIZE;
		uart[COM_RS485_2].rec_null = 0;
		if ((uart[COM_RS485_2].rec_end) == uart[COM_RS485_2].rec_head)
		{
			uart[COM_RS485_2].rec_full = 1;
		}
	}
}
void COM_RS485_2_Tx_IrqCallback(void)
{
	if(1)// while(__HAL_UART_GET_FLAG(&huart8, UART_IT_TXE)==SET)
  {
		if(uart[COM_RS485_2].send_null)
		{
			usart_interrupt_enable(uart[COM_RS485_2].USARTx, USART_TDBE_INT, FALSE);// ��ֹ�����ж�
			uart[COM_RS485_2].txstate=COM_STATE_READY_TX;
		}
		else
		{
			usart_data_transmit(uart[COM_RS485_2].USARTx, uart[COM_RS485_2].send_fifo[uart[COM_RS485_2].send_head]);
			uart[COM_RS485_2].send_head++;
			uart[COM_RS485_2].send_head %= UART_FIFO_SIZE;
			uart[COM_RS485_2].send_full = 0;
			if (uart[COM_RS485_2].send_head == uart[COM_RS485_2].send_end)
			{
				uart[COM_RS485_2].send_null = 1;
			}
		}
	}
}
/***********************************************************************************
Function:       InitCom(uint8_t Port, INT32U Baud, uint8_t DataLen, uint8_t Parity, uint8_t Stop)
Description:    ��ʼ������
Input:          uint8_t Port    ���ں�
                INT32U Baud   ���ڲ�����
                uint8_t DataLen ����λ
                uint8_t Parity  У������	0��У�飬1��У�飬2ż����
                uint8_t Stop    ֹͣλ
Output:         ���������
Return:         ���ش��ڵ�ַ
global variable:ȫ�ֱ�����
Others:         ����˵����
***********************************************************************************/
UART_HANDLER InitCom(uint8_t Port, uint32_t baud,usart_data_bit_num_type wordlen,usart_stop_bit_num_type stopbit,usart_parity_selection_type paritybit)
{
	UART_HANDLER rt = NULL;

	switch(Port)
	{
		case COM_RS232_Debug:
//			COM_RS232_DebugInit(baud,wordlen,stopbit,paritybit);	//UART,bps,parity
			/* configure usart param */
			usart_init(USART2, baud, wordlen, stopbit);
			usart_transmitter_enable(USART2, TRUE);
			usart_receiver_enable(USART2, TRUE);
			/* enable usart interrupt */
			usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
		
			uart[COM_RS232_Debug].send_head = 0;					/*����ͷָ��*/
			uart[COM_RS232_Debug].send_end = 0;					/*����βָ��*/
			uart[COM_RS232_Debug].rec_head = 0;					/*����ͷָ��*/
			uart[COM_RS232_Debug].rec_end = 0;						/*����βָ��*/
			uart[COM_RS232_Debug].send_full = 0;					/*���ͻ�������־*/
			uart[COM_RS232_Debug].send_null = 1;					/*���ͻ���ձ�־*/
			uart[COM_RS232_Debug].rec_full = 0;					/*���ջ�������־*/
			uart[COM_RS232_Debug].rec_null = 1;					/*���ջ���ձ�־*/
			uart[COM_RS232_Debug].USARTx = USART2;
			rt = &uart[COM_RS232_Debug];
			break;
		case COM_RS232:
//			COM_RS232Init(baud,wordlen,stopbit,paritybit);	//UART,bps,parity
			/* configure usart param */
			usart_init(USART1, baud, wordlen, stopbit);
			usart_transmitter_enable(USART1, TRUE);
			usart_receiver_enable(USART1, TRUE);
			/* enable usart interrupt */
			usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
			uart[COM_RS232].send_head = 0;					/*����ͷָ��*/
			uart[COM_RS232].send_end = 0;					/*����βָ��*/
			uart[COM_RS232].rec_head = 0;					/*����ͷָ��*/
			uart[COM_RS232].rec_end = 0;						/*����βָ��*/
			uart[COM_RS232].send_full = 0;					/*���ͻ�������־*/
			uart[COM_RS232].send_null = 1;					/*���ͻ���ձ�־*/
			uart[COM_RS232].rec_full = 0;					/*���ջ�������־*/
			uart[COM_RS232].rec_null = 1;					/*���ջ���ձ�־*/
			uart[COM_RS232].USARTx = USART1;
			rt = &uart[COM_RS232];
			break;
		case COM_RS485_1:
//			COM_RS232Init(baud,wordlen,stopbit,paritybit);	//UART,bps,parity
			/* configure usart param */
			usart_init(USART3, baud, wordlen, stopbit);
			usart_transmitter_enable(USART3, TRUE);
			usart_receiver_enable(USART3, TRUE);
			/* enable usart interrupt */
			usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
			uart[COM_RS485_1].send_head = 0;					/*����ͷָ��*/
			uart[COM_RS485_1].send_end = 0;					/*����βָ��*/
			uart[COM_RS485_1].rec_head = 0;					/*����ͷָ��*/
			uart[COM_RS485_1].rec_end = 0;						/*����βָ��*/
			uart[COM_RS485_1].send_full = 0;					/*���ͻ�������־*/
			uart[COM_RS485_1].send_null = 1;					/*���ͻ���ձ�־*/
			uart[COM_RS485_1].rec_full = 0;					/*���ջ�������־*/
			uart[COM_RS485_1].rec_null = 1;					/*���ջ���ձ�־*/
			uart[COM_RS485_1].USARTx = USART3;
			rt = &uart[COM_RS485_1];
			break;
		case COM_RS485_2:
//			COM_RS485_2Init(baud,wordlen,stopbit,paritybit);	//UART,bps,parity
			/* configure usart param */
			usart_init(USART6, baud, wordlen, stopbit);
			usart_transmitter_enable(USART6, TRUE);
			usart_receiver_enable(USART6, TRUE);
			/* enable usart interrupt */
			usart_interrupt_enable(USART6, USART_RDBF_INT, TRUE);
			uart[COM_RS485_2].send_head = 0;					/*����ͷָ��*/
			uart[COM_RS485_2].send_end = 0;					/*����βָ��*/
			uart[COM_RS485_2].rec_head = 0;					/*����ͷָ��*/
			uart[COM_RS485_2].rec_end = 0;						/*����βָ��*/
			uart[COM_RS485_2].send_full = 0;					/*���ͻ�������־*/
			uart[COM_RS485_2].send_null = 1;					/*���ͻ���ձ�־*/
			uart[COM_RS485_2].rec_full = 0;					/*���ջ�������־*/
			uart[COM_RS485_2].rec_null = 1;					/*���ջ���ձ�־*/
			uart[COM_RS485_2].USARTx = USART6;
			rt = &uart[COM_RS485_2];
			break;
		default:
			break;
	}
	return rt;
}


/********************************************************************
** ��������:	ReadCom
** ��������:	�����ڻ���������
**
** �䡡��:	uint8_t port:���ں�
** �䡡��:	int8_t *strhead:���������׵�ַ,Ӧ���ٿɴ��256���ֽ�����
**			INT16S *num:���������ֽ�����
** ��  ��:	����:int8_t,0:����ǿ�,2:��������,1:���ںŷǷ�
**
** ȫ�ֱ���: uart[]
** ����ģ��: ��
**
**-------------------------------------------------------------------
** �޸���:
** �ա���:
**-----------------------------------------------------------------*/
int8_t ReadCom(int8_t port,uint8_t *strhead,uint16_t * num)
{
	uint16_t i;
	int8_t ret;
	struct UART * p;
	if(port<SYS_COM_NUM)
	{
		p=&uart[port];
		if(p->rec_null==0)
		{
			i=0;
			while((p->rec_null==0)&&(i<256))
			{
				*strhead=p->rec_fifo[p->rec_head];
				strhead++;
				p->rec_head++;
				p->rec_head %= UART_FIFO_SIZE;
				p->rec_full=0;
				if (p->rec_head==p->rec_end)
				{
					p->rec_null=1;
				}
				i++;
			}
			*num = i;
			ret = 0;
		}
		else
		{
			*num = 0;
			ret = 2;
		}
	}
	else
	{
		*num = 0;
		ret = 1;
	}
	return ret;
}

int8_t UartGetChar(int8_t port,uint8_t *strhead){
	uint16_t i;
	int8_t ret;
	struct UART * p;
	if(port<SYS_COM_NUM)
	{
		p=&uart[port];
		if(p->rec_null==0)
		{
			i=0;
			while((p->rec_null==0)&&(i<1))
			{
				*strhead=p->rec_fifo[p->rec_head];
				strhead++;
				p->rec_head++;
				p->rec_head %= UART_FIFO_SIZE;
				p->rec_full=0;
				if (p->rec_head==p->rec_end)
				{
					p->rec_null=1;
				}
				i++;
			}
//			*num = i;
			ret = 0;
		}
		else
		{
//			*num = 0;
			ret = 2;
		}
	}
	else
	{
//		*num = 0;
		ret = 1;
	}
	return ret;
}

/********************************************************************
** ��������:	SendLeave
** ��������:	��ѯ���ͻ����д����͵��ַ�����
**
** �䡡��:	comport:��Ҫ��ѯ���ں�
** �䡡��:	��
** ��  ��:	����ֵstΪ���ͻ����д����͵��ַ�����,0:��;0xff:���
**
** ȫ�ֱ���: ��
** ����ģ��: ��
**
**-------------------------------------------------------------------
** �޸���:
** �ա���:
**-----------------------------------------------------------------*/
uint16_t SendLeave(uint8_t comport)
{
    uint16_t st;
    if(uart[comport].send_full)
    {
		st = UART_FIFO_SIZE;
	}
	else if(uart[comport].send_null)
	{
		st = 0;
    }
    else
    {
//		OS_ENTER_CRITICAL();
		st=(uart[comport].send_end+UART_FIFO_SIZE-uart[comport].send_head)%UART_FIFO_SIZE;
//		OS_EXIT_CRITICAL();
	}
	return st;
}

/********************************************************************
** ��������:	SendBuffNum
** ��������:	��ѯ���ͻ�����ʣ��ռ�����
**
** �䡡��:	comport:��Ҫ��ѯ���ں�
** �䡡��:	��
** ��  ��:	����ֵstΪ���ͻ�����ʣ��ռ�����,0xff:���ںŴ�
**
** ȫ�ֱ���: ��
** ����ģ��: ��
**
**-------------------------------------------------------------------
** �޸���:
** �ա���:
**-----------------------------------------------------------------*/
uint16_t SendBuffNum(uint8_t port)
{
	uint16_t st;
	if(port>=SYS_COM_NUM)
	{
		st = 0xffff;
	}
	else
	{
		st=UART_FIFO_SIZE-SendLeave(port);
	}
	return st;
}

/********************************************************************
** ��������:	WriteCom
** ��������:	д���ݵ����ڷ��ͻ�����
**
** �䡡��:	uint8_t port:���ں�
**			int8_t *strhead:�����������׵�ַ
**			INT16S *num:�����������ֽ�����
** �䡡��:	��
** ��  ��:	����:int8_t,	0:д�������ɹ�,3:�������ռ䲻��,
**						2:�����������ֽ�����Ϊ0,1:���ںŷǷ�
**
** ȫ�ֱ���: uart[]
** ����ģ��: ��
** �޸���:
** �ա���:
**-----------------------------------------------------------------*/
int8_t WriteCom(int8_t port,uint8_t * strhead,uint16_t num)
{
	uint16_t i;
	uint16_t temp;
	int8_t ret;
	struct UART * p;
	temp=SendBuffNum(port);
	if((port>=SYS_COM_NUM)||(temp==0xffff))
	{
		ret = 1;
	}
	else if(num == 0)
	{
		ret = 2;
	}
	else if(temp<num)
	{
		ret = 3;
	}
	else
	{
//		OS_ENTER_CRITICAL();
		p=&uart[port];
		for(i=0;i<num;i++)
		{
			p->send_fifo[p->send_end]=*(strhead+i);
			p->send_end++;
			p->send_end %= UART_FIFO_SIZE;
			p->send_null=0;
			if(p->send_end==p->send_head)
			{
//				p->send_full=1;
			}
		}
		if(p->txstate==COM_STATE_BUSY_TX)
		{
			ret = 0;
			return ret;
		}
	
		if (usart_flag_get(p->USARTx, USART_TDBE_FLAG)==SET)
		{
			usart_interrupt_enable(p->USARTx, USART_TDBE_INT, TRUE);
			p->txstate=COM_STATE_BUSY_TX;
//			usart_data_transmit(p->USARTx, p->send_fifo[p->send_head]);
//			USART_UART_Trans(p->USARTx, &p->send_fifo[p->send_head], 1, 5000);
////			HAL_UART_Transmit_IT(p->USARTx, &p->send_fifo[p->send_head], 1);
////			p->USARTx->DR = p->send_fifo[p->send_head];
//			p->send_head++;
//			p->send_head %= UART_FIFO_SIZE;
//			p->send_full = 0;
//			if(p->send_end==p->send_head)
//			{
//				p->send_null = 1;
//			}
		}
		ret = 0;
	}
	return ret;
}
/* USER CODE BEGIN Header_StartTaskCom */
/**
* @brief Function implementing the ComTask thread.
* @param argument: Not used
* @retval None
*/
#include <string.h>
#include "rtc.h"
#include "./logic/bsp_logic.h"
#include "./modbus/bsp_modbus.h"
#include <stdlib.h>
uint8_t rxdata[MODBUS_FRAME_LEN];
uint16_t rxlen;
uint8_t txdata[MODBUS_DATA_LEN];
extern RTC_RtcDef RTC_Rtc;
extern stc_modbus stcModbus;
/* USER CODE END Header_StartTaskCom */
void StartTaskCom(void *argument)
{	
	ModbusInit();
	InitCom(COM_RS232_Debug, 9600,USART_DATA_8BITS,USART_STOP_1_BIT,USART_PARITY_NONE);
	InitCom(COM_RS232, 9600,USART_DATA_8BITS,USART_STOP_1_BIT,USART_PARITY_NONE);
	InitCom(COM_RS485_1, 9600,USART_DATA_8BITS,USART_STOP_1_BIT,USART_PARITY_NONE);
	InitCom(COM_RS485_2, 9600,USART_DATA_8BITS,USART_STOP_1_BIT,USART_PARITY_NONE);
#define MSG_com "com口初始化完成"	

	WriteCom(COM_RS232_Debug,(uint8_t *)MSG_com,strlen(MSG_com));

  /* Infinite loop */
  for(;;)
  {	int i=0;
    // ReadCom(COM_RS232_Debug,stcModbus.frame,&stcModbus.frame_len);
	ReadCom(COM_RS232_Debug,rxdata,&rxlen);	
    osDelay(10);
	if (rxlen>6)
		{
		uint8_t reslut=M_ERROR;
		//将redata写入结构体stcModbus.frame中
		memcpy(stcModbus.frame, rxdata, rxlen);
		stcModbus.frame_len=rxlen;
		reslut=S_ModbusParseData(&stcModbus);
		if(reslut==M_SUCCESS)
			{
				S_ModbusMakeData(&stcModbus);
				WriteCom(COM_RS232_Debug,stcModbus.frame,stcModbus.frame_len);
				memset(&stcModbus, 0, sizeof(stc_modbus));
			}
		else {
			WriteCom(COM_RS232_Debug,(uint8_t *)"无效数据",8);
			memset(&stcModbus, 0, sizeof(stc_modbus));

			}	
			}

    ReadCom(COM_RS232,rxdata,&rxlen);
    osDelay(10);
    if(rxlen>0) 
			WriteCom(COM_RS232,rxdata,rxlen);
    osDelay(10);
    ReadCom(COM_RS485_1,rxdata,&rxlen);
    osDelay(10);
		if(rxlen>0)    
			WriteCom(COM_RS485_1,rxdata,rxlen);
    osDelay(10);
    ReadCom(COM_RS485_2,rxdata,&rxlen);
    osDelay(10);
		if(rxlen>0)    
			WriteCom(COM_RS485_2,rxdata,rxlen);
		for(i=0;i<MODBUS_FRAME_LEN;i++) rxdata[i]=0;

		rxlen=0;
    osDelay(10);
  }
  /* USER CODE END StartTaskCom */
}


/* suport printf function, usemicrolib is unnecessary */
//#if (__ARMCC_VERSION > 6000000)
//  __asm (".global __use_no_semihosting\n\t");
//  void _sys_exit(int x)
//  {
//    x = x;
//  }
//  /* __use_no_semihosting was requested, but _ttywrch was */
//  void _ttywrch(int ch)
//  {
//    ch = ch;
//  }
//  FILE __stdout;
//#else
// #ifdef __CC_ARM
//  #pragma import(__use_no_semihosting)
//  struct __FILE
//  {
//    int handle;
//  };
//  FILE __stdout;
//  void _sys_exit(int x)
//  {
//    x = x;
//  }
// #endif
//#endif
// 
//#if defined ( __GNUC__ ) && !defined (__clang__)
//  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
//#else
//  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
//#endif
// 
///**
//* @brief  retargets the c library printf function to the usart.
//* @param  none
//* @retval none
//*/
//PUTCHAR_PROTOTYPE
//{
//	int timeout = 1000; // 设置超时时间
//	while(usart_flag_get(COM_RS232_Debug, USART_TDBE_FLAG) == RESET) {
//		if (--timeout == 0) {
//			// 重置usart
////			usart_deinit(COM_RS232_Debug);
//			usart_init(COM_RS232_Debug, 9600, USART_DATA_8BITS, USART_STOP_1_BIT);
//			WriteCom(COM_RS232_Debug,(uint8_t *)"1",1);
//			break;
//		}
//		}
//  usart_data_transmit(COM_RS232_Debug, ch);
////	
////	WriteCom(COM_RS232_Debug,ch,1);
//  return ch;
//}
